package test.administrator.example.com.mvptest1.framework.support.viewstate.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import test.administrator.example.com.mvptest1.framework.MvpPresenter;
import test.administrator.example.com.mvptest1.framework.MvpView;
import test.administrator.example.com.mvptest1.framework.support.activity.ActivityMvpDelegateImpl;
import test.administrator.example.com.mvptest1.framework.support.manager.PresenterManager;
import test.administrator.example.com.mvptest1.framework.support.viewstate.RestorableViewState;
import test.administrator.example.com.mvptest1.framework.support.viewstate.ViewState;
import test.administrator.example.com.mvptest1.framework.support.viewstate.MvpViewStateDelegateCallback;

/**
 * 代理一：针对于生命周期->目标对象
 * Created by thinkpad on 2018/8/6.
 */
public class ActivityMvpViewStateDelegateImpl<V extends MvpView, P extends MvpPresenter<V>, VS extends ViewState<V>> extends ActivityMvpDelegateImpl<V, P> {

	private Activity activity;
	private MvpViewStateDelegateCallback<V, P, VS> delegateCallback;
	private boolean keepPresenterInstance;

	public ActivityMvpViewStateDelegateImpl(Activity activity, MvpViewStateDelegateCallback<V, P, VS> delegateCallback, boolean keepPresenterInstance) {
		super(activity, delegateCallback, keepPresenterInstance);
		this.activity = activity;
		this.delegateCallback = delegateCallback;
		this.keepPresenterInstance = keepPresenterInstance;
	}

	private void setViewState(@NonNull VS viewState, boolean applyViewState, boolean applyViewStateFromMemory) {
		if (viewState == null) {
			throw new NullPointerException("viewState不能为空");
		}
		delegateCallback.setViewState(viewState);
		if (applyViewState) {
			//恢复
			delegateCallback.setRestoringViewState(true);
			//调用具体的备份方法(一般由子类决定)
			delegateCallback.getViewState().apply(delegateCallback.getMvpView(), applyViewStateFromMemory);
			delegateCallback.setRestoringViewState(false);
			delegateCallback.onViewStateInstanceRestored(applyViewStateFromMemory);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		boolean keepInstance = retainPresenterInstance(keepPresenterInstance, activity);
		VS viewState = delegateCallback.getViewState();
		if (viewState == null) {
			throw new NullPointerException("viewState不能为空");
		}
		//存在P层，并且viewState是恢复状态，那么需要缓存
		if (keepInstance && viewState instanceof RestorableViewState) {
			((RestorableViewState) viewState).onSaveInstanceState(outState);
		}
	}

	@Override
	public void onPostCreate(@Nullable Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		VS viewState = null;
		if (viewId != null) {
			viewState = PresenterManager.getViewState(activity, viewId);
			if (viewState != null) {
				//缓存
				setViewState(viewState, true, true);
				return;
			}
		}
		//如果为空，创建一个ViewState
		viewState = delegateCallback.createViewState();
		if (viewState == null) {
			throw new NullPointerException("你没有创建viewState，需要创建");
		}
		if (savedInstanceState != null && viewState instanceof RestorableViewState) {
			RestorableViewState restorableViewState = (RestorableViewState) viewState;
			restorableViewState.onRestoreInstanceState(savedInstanceState);
			if (restorableViewState != null) {
				viewState = (VS) restorableViewState;
				setViewState(viewState, true, false);
				if (keepPresenterInstance) {
					if (viewId == null) {
						throw new NullPointerException("viewId不能为空");
					}
					PresenterManager.putViewState(activity, viewId, viewState);
				}
				return;
			}
		}
		if (keepPresenterInstance) {
			if (viewId == null) {
				throw new NullPointerException("viewId不能为空");
			}
			PresenterManager.putViewState(activity, viewId, viewState);
		}
		//恢复完毕
		setViewState(viewState, false, false);
		//创建新的缓存实例对象为下一次备份使用
		delegateCallback.onNewViewStateInstance();
	}

}




























































































